package com.ly.lypopupwindowdemo.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.pop.SlideFromTopPopup;

import razerdp.basepopup.BasePopupWindow;

public class FromTopActivity extends AppCompatActivity {

    private SlideFromTopPopup mSlideFromTopPopup;
    private RelativeLayout titleParent;

    private ImageView arrow;
    private RotateAnimation showArrowAnima;
    private RotateAnimation dismissArrowAnima;

    private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(FromTopActivity.this, "点击position"+position, Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        buildShowArrowAnima();
        buildDismissArrowAnima();
        setContentView(R.layout.activity_from_top);

        titleParent = (RelativeLayout) findViewById(R.id.rl_title);

        arrow = (ImageView) findViewById(R.id.iv_arrow);

        titleParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPop();
            }
        });

    }

    private void showPop() {
        mSlideFromTopPopup = new SlideFromTopPopup(FromTopActivity.this,listener);
        mSlideFromTopPopup.setOnDismissListener(onDismissListener);
        mSlideFromTopPopup.setOnBeforeShowCallback(new BasePopupWindow.OnBeforeShowCallback() {
            @Override
            public boolean onBeforeShow(View popupRootView, View anchorView, boolean hasShowAnima) {
                if (anchorView!=null) {
                    mSlideFromTopPopup.setOffsetY(anchorView.getHeight());
                    return true;
                }
                return false;
            }
        });

        if (!mSlideFromTopPopup.isShowing()) startShowArrowAnima();
        mSlideFromTopPopup.showPopupWindow(titleParent);



    }


    private BasePopupWindow.OnDismissListener onDismissListener =
            new BasePopupWindow.OnDismissListener() {

                @Override
                public boolean onBeforeDismiss() {
                    startDismissArrowAnima();
                    return super.onBeforeDismiss();
                }

                @Override
                public void onDismiss() {

                }
            };
    private void startShowArrowAnima() {
        if (arrow == null) return;
        arrow.clearAnimation();
        arrow.startAnimation(showArrowAnima);
    }

    private void startDismissArrowAnima() {
        if (arrow == null) return;
        arrow.clearAnimation();
        arrow.startAnimation(dismissArrowAnima);
    }

    private void buildShowArrowAnima() {
        if (showArrowAnima != null) return;
        showArrowAnima = new RotateAnimation(0, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        showArrowAnima.setDuration(450);
        showArrowAnima.setInterpolator(new AccelerateDecelerateInterpolator());
        showArrowAnima.setFillAfter(true);
    }

    private void buildDismissArrowAnima() {
        if (dismissArrowAnima != null) return;
        dismissArrowAnima = new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        dismissArrowAnima.setDuration(450);
        dismissArrowAnima.setInterpolator(new AccelerateDecelerateInterpolator());
        dismissArrowAnima.setFillAfter(true);
    }


}
